package com.scc.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.scc.pojo.Result;
import com.scc.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
@Slf4j
public class Interceptor implements HandlerInterceptor {

    //连接前执行拦截，返回true通过拦截(jwt令牌合法)，返回false没有通过拦截(jwt出问题)
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //书写jwt验证操作
        ///获取请求的路径，如果是登录就直接放行
        String url = request.getRequestURI();
        log.info("请求的路径是:{}", url);
        ///请求路径包含login
        if(url.contains("login")){
            log.info("请求的路径是登录操作,放行");
            return true;
        }
        ///从请求头中获取jwt令牌
        String jwt = request.getHeader("token");
        ///判断是什么身份的登录对象---》从url入手
        ///管理员登录-路径里面有admin
        if(url.contains("admin")){
            //解析jwt,如果jwt为bull直接返回ADMIN_NOT_LOGIN
            if(jwt==null){
               String adminNotLoginMsg = changeErrorMsgToJSON("ADMIN_NOT_LOGIN");
               //响应回去
               response.getWriter().write(adminNotLoginMsg);
               log.info("管理员jwt令牌为null");
               return false;
            }
            //令牌不为空--》解析令牌-->解析令牌不出错说明令牌有效
            try {
                JwtUtil.parseJwt(jwt);
            } catch (Exception e) {
                //解析令牌出错，返回错误信息
                String adminNotLoginMsg = changeErrorMsgToJSON("ADMIN_NOT_LOGIN");
                response.getWriter().write(adminNotLoginMsg);
                return false;
            }
            ///不出错说明令牌合法放行
            log.info("管理员请求令牌合法，放行");
        }else{///请求路径不含admin的，就是员工的请求路径
            if(jwt==null){
                String empNotLoginMsg = changeErrorMsgToJSON("EMP_NOT_LOGIN");
                response.getWriter().write(empNotLoginMsg);
                log.info("员工jwt令牌为null");
                return false;
            }
            ///JWT不为空，解析
            try {
                JwtUtil.parseJwt(jwt);
            } catch (Exception e) {
                String empNotLoginMsg = changeErrorMsgToJSON("EMP_NOT_LOGIN");
                response.getWriter().write(empNotLoginMsg);
                return false;
            }
            log.info("员工请求令牌合法，放行");
        }
        return true;
    }
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
    }

    ///这是一个将装有错误信息的返回结果转化为json格式的方法
    public String changeErrorMsgToJSON(String errorMsg) {
        //返回错误信息的对象
        Result error = Result.error(errorMsg);
        //调用json库的方法,转化之后直接返回
        return JSONObject.toJSONString(error);
    }

}
